<template>
  <el-select
    v-model="childValue"
    clearable
    style="width: 100%;"
    @change="childInputChange"
    @visible-change="vChange"
  >
    <el-row style="margin: 2px">
      <el-col :span="19">
        <el-input v-model="query" size="mini" placeholder="请输入" @keyup.enter.native="getPageData()" />
      </el-col>
      <el-col :span="5">
        <el-button icon="el-icon-search" size="mini" @click="getPageData()" />
      </el-col>
    </el-row>
    <el-option
      v-for="(item, index) in options"
      :key="index"
      :label="item.label"
      :value="item.value"
    />
    <el-pagination
      :page-size="size"
      small
      layout="prev, pager, next"
      :total="total"
      :current-page="current"
      @current-change="handleCurrentChange"
    />
    <el-row style="margin: 5px;color:burlywood;font-size: 2px;text-align: center">可输入筛选</el-row>
  </el-select>
</template>

<script>
import { getByDictCode } from '@/api/dict'
import { formatData } from '@/utils/ParamFilter'

export default {
  name: 'BeeSelect',
  model: {
    prop: 'parentValue',
    event: 'childValueChange'
  },
  props: {
    parentValue: {
      type: String,
      default: () => {
        return undefined
      }
    },
    dictCode: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      query: undefined,
      size: 3,
      total: 0,
      current: 0,
      childValue: this.parentValue,
      options: [
      ]
    }
  },
  watch: {
    parentValue(val) {
      this.childValue = val
      if (val !== null && val !== '' && val !== undefined) {
        this.getPageData(val)
      }
    }
  },
  mounted() {
    this.getPageData(this.childValue)
  },
  methods: {
    getPageData(val) {
      var query
      if (val === undefined || val === '') {
        query = this.query
      } else {
        query = val
      }
      getByDictCode({
        current: this.current,
        size: this.size,
        dictCode: this.dictCode,
        label: query
      }).then(res => {
        this.options = formatData(res.data.records)
        this.total = res.data.total
      }).catch(err => {
        console.log(err)
      }).finally(() => {
      })
    },
    handleCurrentChange(current) {
      this.current = current
      this.getPageData()
    },
    childInputChange() {
      this.$emit('childValueChange', this.childValue)
    },
    vChange(flag) {
      this.current = 0
      if (flag) {
        this.childValue = undefined
        this.query = undefined
      }
      this.getPageData()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
